package org.tloss.web.editor.database.editors;

import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.layout.TreeColumnLayout;
import org.eclipse.jface.viewers.ColumnLabelProvider;
import org.eclipse.jface.viewers.ColumnWeightData;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.jface.viewers.TreeViewerColumn;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeColumn;
import org.eclipse.swt.widgets.TreeItem;
import org.tloss.web.editor.database.editors.jdbc.Schema;
import org.tloss.web.editor.model.Database;

public class SchemaListDlg extends Dialog {

	Database database;

	TreeViewer treeViewer;

	protected SchemaListDlg(Shell parentShell, Database database) {
		super(parentShell);
		this.database = database;
	}

	protected Point getInitialSize() {
		return new Point(510, 540);
	}

	@Override
	protected Control createDialogArea(Composite parent) {

		Composite treeContainer = new Composite(parent, SWT.BORDER);
		GridData gridData = new GridData();
		treeContainer.setLayoutData(gridData);
		gridData.horizontalAlignment = SWT.FILL;
		gridData.grabExcessHorizontalSpace = true;
		gridData.verticalAlignment = SWT.FILL;
		gridData.grabExcessVerticalSpace = true;
		TreeColumnLayout treeLayout = new TreeColumnLayout();
		treeContainer.setLayout(treeLayout);
		treeViewer = new TreeViewer(treeContainer, SWT.MULTI
				| SWT.FULL_SELECTION);
		Tree tree = treeViewer.getTree();
		tree.setHeaderVisible(true);
		TreeColumn keyColumn;
		TreeColumn valueColumn;

		keyColumn = new TreeColumn(tree, SWT.NONE);
		keyColumn.setText("Schema");
		treeLayout.setColumnData(keyColumn, new ColumnWeightData(2));

		valueColumn = new TreeColumn(tree, SWT.NONE);
		valueColumn.setText("Catalog");
		treeLayout.setColumnData(valueColumn, new ColumnWeightData(2));

		treeViewer.setContentProvider(new SchemaListContentProvider());
		final SchemaLableProvider treeLabelProvider = new SchemaLableProvider();
		treeViewer.setLabelProvider(treeLabelProvider);

		TreeViewerColumn column1 = new TreeViewerColumn(treeViewer, keyColumn);

		column1.setLabelProvider(new ColumnLabelProvider() {
			public String getText(Object element) {
				return treeLabelProvider.getColumnText(element, 0);
			}

			public Image getImage(Object element) {

				return treeLabelProvider.getColumnImage(element,0);
			}
		});

		TreeViewerColumn column2 = new TreeViewerColumn(treeViewer, valueColumn);

		column2.setLabelProvider(new ColumnLabelProvider() {
			public String getText(Object element) {
				return treeLabelProvider.getColumnText(element, 1);
			}

			public Image getImage(Object element) {

				return treeLabelProvider.getColumnImage(element,1);
			}
		});
		treeViewer.setInput(database);

		treeViewer.setAutoExpandLevel(TreeViewer.ALL_LEVELS);
		return treeContainer;
	}

	protected void okPressed() {
		TreeItem[] items = treeViewer.getTree().getSelection();
		if (items != null && items.length > 0) {
			TreeItem item = items[0];
			database.setSchema((Schema) item.getData());
		}
		super.okPressed();
	}
}
